package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.InformationReply;
import com.caocaog.market.mapper.InformationReplyMapper;
import com.caocaog.market.service.InformationReplyService;
import com.caocaog.market.service.InformationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.CheckFailedException;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 内容回复
 *
 * @author chechen
 * @date 2021-11-13
 */
@Service
@Transactional
public class InformationReplyServiceImpl extends ServiceImpl<InformationReplyMapper, InformationReply> implements InformationReplyService {

    private final InformationService informationService;

    public InformationReplyServiceImpl(@Lazy InformationService informationService) {
        this.informationService = informationService;
    }

    @Override
    public IPage<InformationReply> queryInformationReplyPage(Page<InformationReply> page, InformationReply informationReply) {
        if (StringUtils.isBlank(informationReply.getInfoId()))
            throw new CheckFailedException("信息发布id（infoId）不能为空");
        return super.baseMapper.selectPage(page, new LambdaQueryWrapper<InformationReply>()
                .eq(InformationReply::getInfoId, informationReply.getInfoId()));
    }

    @Override
    public String addInformationReply(InformationReply informationReply) {
        informationReply.setCreateTime(new Date());
        informationReply.setCreateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());
        informationReply.setDelFlag(CommonConstant.DEL_FLAG_0);

        // 信息回复+1
        this.informationService.replyQtyPlusOne(informationReply.getInfoId());
        super.baseMapper.insert(informationReply);
        if (informationReply.getId() == null)
            throw new JeecgBootException("新增回复内容失败");
        return informationReply.getId();
    }

    @Override
    public int deleteInformationReplyById(String id) {
        int deleteRecordCount = super.baseMapper.deleteById(id);
        if (deleteRecordCount == 0)
            throw new JeecgBootException("删除回复内容失败, id-> " + id);
        return deleteRecordCount;
    }

    @Override
    public int deleteBatchInformationReplyByIds(List<String> ids) {
        int deleteRecordCount = super.baseMapper.deleteBatchIds(ids);
        if (deleteRecordCount < ids.size())
            throw new JeecgBootException(
                    String.format("批量删除回复内容失败, deleteRecordCount-> %s, ids.size()-> %s, ids-> %s",
                            deleteRecordCount, ids.size(), ids));
        return deleteRecordCount;
    }

    @Override
    public int deleteInformationReplyByInfoId(String infoId) {
        return super.baseMapper.delete(new LambdaQueryWrapper<InformationReply>()
                .eq(InformationReply::getInfoId, infoId));
    }

    @Override
    public int deleteBatchInformationReplyByInfoIds(List<String> infoIds) {
        return super.baseMapper.delete(new LambdaQueryWrapper<InformationReply>()
                .in(InformationReply::getInfoId, infoIds));
    }
}
